package com.jinzecloud.blog.controller;

import com.jinzecloud.blog.bean.Tag;
import com.jinzecloud.blog.service.TagService;
import com.jinzecloud.blog.utils.Page;
import com.jinzecloud.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * 标签 
 *
 * @author kumi
 * @email 1971240051@qq.com
 * @date 2022-06-04 03:23:06
 */
@RestController
@RequestMapping("article/tag")
public class TagController {

    @Autowired
    TagService tagService;

    @RequestMapping("/add")
    public Result addTag(@RequestBody Tag tag){
        try{
            tagService.add(tag);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"添加失败");
        }

        return Result.ok().put("200","添加成功");
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Tag tag){
        try{
            tagService.update(tag);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"修改失败");
        }

        return Result.ok().put("200","修改成功");
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody Integer[] ids){
        try{
            tagService.delete(ids);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"删除失败");
        }

        return Result.ok().put("200","删除成功");
    }

    @RequestMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Tag tag = null;
        try{
            tag = tagService.getByTagId(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"查询失败");
        }

        return Result.ok().put("data",tag);
    }

    @RequestMapping("/getByIdAll")
    public Result getByIdAll(){
        Page<Tag> tag = null;
        try{
            tag = tagService.getByIdAll();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,"查询失败");
        }

        return Result.ok().put("data",tag);
    }
}
